package cn.edu.sgu.www.mhxysy.controller.qiling;

import cn.edu.sgu.www.mhxysy.vo.Imager;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.qiling.QilingDTO;
import cn.edu.sgu.www.mhxysy.entity.qiling.Qiling;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.qiling.QilingService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "器灵管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/qiling", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class QilingController {

	private final QilingService qilingService;

	@Autowired
	public QilingController(QilingService qilingService) {
		this.qilingService = qilingService;
	}

	@ApiOperation("计算器灵评分")
	@RequestMapping(value = "/compute", method = RequestMethod.POST)
	public JsonResult<Void> compute(
			@ApiParam(name = "qilingId", value = "器灵ID", required = true)
			@NotBlank(message = CustomMessages.QILING_ID_NOT_NULL) String qilingId) {
		qilingService.compute(qilingId);

		return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
	}

	@ApiOperation("器灵镶嵌")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "qilingId", value = "器灵ID", required = true),
			@ApiImplicitParam(name = "equipmentId", value = "装备ID", required = true)
	})
	@RequestMapping(value = "/inlay", method = RequestMethod.POST)
	public JsonResult<Void> inlay(
			@NotBlank(message = CustomMessages.QILING_ID_NOT_NULL) String qilingId,
			@NotBlank(message = CustomMessages.EQUIPMENT_ID_NOT_NULL) String equipmentId) {
		String operation = qilingService.inlay(qilingId, equipmentId);

		return JsonResult.success(operation + "成功");
	}

	@ApiOperation("添加器灵")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated QilingDTO qilingDTO) {
		qilingService.insert(qilingDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID删除器灵")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "器灵ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		qilingService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改器灵信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated QilingDTO qilingDTO) {
		qilingService.updateById(qilingDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("通过ID查询器灵信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<Qiling> selectById(
			@ApiParam(name = "id", value = "器灵ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		Qiling qiling = qilingService.selectById(id);

		return JsonResult.success(null, qiling);
	}

	@ApiOperation("分页查询器灵列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<Qiling>> selectByPage(Pager<Qiling> pager) {
		Page<Qiling> page = qilingService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

	@ApiOperation("查询装备镶嵌的器灵")
	@RequestMapping(value = "/selectImage", method = RequestMethod.GET)
	public JsonResult<Imager> selectImage(
			@ApiParam(name = "equipmentId", value = "装备ID", required = true)
			@NotBlank(message = CustomMessages.EQUIPMENT_ID_NOT_NULL) String equipmentId) {
		Imager imager = qilingService.selectImage(equipmentId);

		return JsonResult.success(null, imager);
	}

	@ApiOperation("查询装备已镶嵌的器灵")
	@RequestMapping(value = "/selectByEquipmentId", method = RequestMethod.GET)
	public JsonResult<Qiling> selectByEquipmentId(
			@ApiParam(name = "equipmentId", value = "装备ID", required = true)
			@NotBlank(message = CustomMessages.EQUIPMENT_ID_NOT_NULL) String equipmentId) {
		Qiling qiling = qilingService.selectByEquipmentId(equipmentId);

		return JsonResult.success(null, qiling);
	}

	@ApiOperation("查询器灵列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roleId", value = "角色ID", required = false),
			@ApiImplicitParam(name = "equipmentId", value = "装备类型ID", required = false)
	})
	@RequestMapping(value = "/selectList", method = RequestMethod.GET)
	public JsonResult<List<Qiling>> selectList(
			@RequestParam(required = false) String roleId,
			@RequestParam(required = false) Integer equipmentId) {
		List<Qiling> list = qilingService.selectList(roleId, equipmentId);

		return JsonResult.success(null, list);
	}

}